﻿
var instance = [{ "ID": "00000000-0000-0000-0000-000000000000", "CustomerName": "", "Address": "", "State": "",
    "Province": "", "City": "", "Area": "", "Decisionmaker": "", "DecisionPosition": "", "DecisionTel1": "",
    "DecisionTel2": "", "DecisionSex": "", "CompanyProduct": "", "Tel1": "",
    "PersonId": "00000000-0000-0000-0000-000000000000", "PersonName": "",
    "AreaId": "00000000-0000-0000-0000-000000000000", "AreaName": "",
    "ProductId": "00000000-0000-0000-0000-000000000000", "ProductName": "",
    "TradeId": "00000000-0000-0000-0000-000000000000", "TradeName": "",
    "PropertyId": "00000000-0000-0000-0000-000000000000", "PropertyName": "", "Explains": ""
}];


YAHOO.namespace("example.container");
YAHOO.util.Event.onDOMReady(function () {
    var handleSubmit1 = function () {
        var editwindow = document.getElementById("EditUIWindow1");
        if (editwindow) {
            editwindow.contentWindow.Save();
        }
    };
    var handleCancel = function () {
        this.cancel();
    };
    var handleSubmit7 = function () {
        var editwindow = document.getElementById("EditUIWindow6");
        if (editwindow) {
            editwindow.contentWindow.ChooseInfo();
        }
        YAHOO.example.container.dialog6.hide();
    }
    var handleSuccess = function (o) {
        var response = o.responseText;
        response = response.split("<!")[0];
        document.getElementById("resp").innerHTML = response;
    };
    var handleFailure = function (o) {
        alert("Submission failed: " + o.status);
    };
    YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1",
	{ width: "400px",
		height: "350px",
		xy: [10, 10],
		fixedcenter: true,
		visible: false,
		draggable: false,
		modal: true,
		close: true,
		autofillheight: "body",
		constraintoviewport: true,
		buttons: [{ text: "确定", handler: handleSubmit1, isDefault: true },
				{ text: "取消", handler: handleCancel}]
	});
	YAHOO.example.container.dialog6 = new YAHOO.widget.Dialog("dialog6",
	{ width: "650px",
		height: "350px",
		xy: [10, 10],
		fixedcenter: true,
		visible: false,
		draggable: false,
		modal: true,
		close: true,
		autofillheight: "body",
		constraintoviewport: true,
		buttons: [{ text: "确定", handler: handleSubmit7, isDefault: true },
				{ text: "取消", handler: handleCancel}]
	});
    YAHOO.example.container.dialog1.callback = { success: handleSuccess, failure: handleFailure };
    YAHOO.example.container.dialog1.render();
    YAHOO.example.container.dialog6.render();
});

//客户区域对应的方法和ID
var _fn, _id;
function ShowDialog(fn, id, src) {
    _fn = fn;
    _id = id;
    document.getElementById("EditUIWindow6").src = src;
    YAHOO.example.container.dialog6.show();
}

$(document).ready(function () {
    if (flag == "0") {
        $("#btnRubbish").hide();
        $("#btnStop").hide();
    }
    if (type == "stop") {
        $("#btnStop").hide();
    }
    //决策人手机只能为数字
    $("#DecisionTel").keypress(function (e) {
        //注意此处不要用keydown方法，否则不能禁用Ctrl+C与Ctrl+V,具体原因请自行查找keyPress与keyDown区分，十分重要，请细查
        if ($.browser.msie) {  //判断浏览器
            if (((event.keyCode > 47) && (event.keyCode < 58)) || (event.keyCode == 8)) { //判断键值 
                return true;
            } else {
                return false;
            }
        } else {
            if (((e.which > 47) && (e.which < 58)) || (e.which == 8) || (event.keyCode == 17)) {
                return true;
            } else {
                return false;
            }
        }
    }).focus(function () {
        this.style.imeMode = 'disabled'; //禁用输入法,禁止输入中文字符
    }).bind("paste", function () {
        //alert($("#text").val());
        //return false;
    }).keyup(function (e) {
        _v = $("#DecisionTel").val();
        for (_i = 0; _i < _v.length; _i++) {
            _c = _v.charAt(_i);
            if (isNaN(_c)) {
                $("#DecisionTel").val("");
                return;
            }
        }
    });
    $("#DecisionTel2").keypress(function (e) {
        //注意此处不要用keydown方法，否则不能禁用Ctrl+C与Ctrl+V,具体原因请自行查找keyPress与keyDown区分，十分重要，请细查
        if ($.browser.msie) {  //判断浏览器
            if (((event.keyCode > 47) && (event.keyCode < 58)) || (event.keyCode == 8)) { //判断键值 
                return true;
            } else {
                return false;
            }
        } else {
            if (((e.which > 47) && (e.which < 58)) || (e.which == 8) || (event.keyCode == 17)) {
                return true;
            } else {
                return false;
            }
        }
    }).focus(function () {
        this.style.imeMode = 'disabled'; //禁用输入法,禁止输入中文字符
    }).bind("paste", function () {
        //alert($("#text").val());
        //return false;
    }).keyup(function (e) {
        _v = $("#DecisionTel2").val();
        for (_i = 0; _i < _v.length; _i++) {
            _c = _v.charAt(_i);
            if (isNaN(_c)) {
                $("#DecisionTel2").val("");
                return;
            }
        }
    });

    $('#Province').change(function () {
        var vProvince = $(this).children('option:selected').val();
        if (vProvince != "") {
            document.getElementById("City").options.length = 0;
            document.getElementById("Area").options.length = 0;
            $("#City").append("<option value=''>选择城市</option>");
            $("#Area").append("<option value=''>选择区县</option>");
            var vc = "ProvinceId='" + vProvince + "'";
            YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/JSONList.aspx?type=1&entity=SysCity&customCondition=" + vc + "", //条件 
                            {success: function (res) {
                                try {
                                    var str = res.responseText;
                                    var obj = eval(str);
                                    for (var i = 0; i < obj.length; i++) {
                                        if (obj[i].Code != undefined || obj[i].Name != undefined) {
                                            $("#City").append("<option value='" + obj[i].Code + "'>" + obj[i].Name + "</option>");
                                        }
                                    }
                                }
                                catch (e) {
                                }
                                finally {
                                }
                            }, failure: function () { }
                        });
        }
    });
    $('#City').change(function () {
        var vCity = $('#City').children('option:selected').val();
        if (vCity != "") {
            document.getElementById("Area").options.length = 0;
            $("#Area").append("<option value=''>选择区县</option>");
            var vc = "CityId='" + vCity + "'";
            YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/JSONList.aspx?type=1&entity=SysArea&customCondition=" + vc + "", //条件 
                            {success: function (res) {
                                try {
                                    var str = res.responseText;
                                    var obj = eval(str);
                                    for (var i = 0; i < obj.length; i++) {
                                        $("#Area").append($("<option></option>").val(obj[i].Name).html(obj[i].Name));
                                    }
                                }
                                catch (e) {
                                }
                                finally {
                                }
                            }, failure: function () { }
                        });
        }
    });
});

//检查客户名是否已存在
function CheckCustomerName() {
    var id = "00000000-0000-0000-0000-000000000000";
    var vName = $('#CustomerName').val();
    if (vName != "") {
        if (vName.length < 8) {
            alert("客户名的长度不能少于8个字,请输入公司全称!\n如：深圳市中旭企业管理股份有限公司");
            $('#CustomerName').focus();
            return;
        } else {
            if (flag != "0") {
                id = $("#ID").val();
            }
            YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/CustomOperating.aspx?r=" + Math.random() + "&id=" + id + "&temp=" + flag + "&type=0&entity=CheckCustomerOld&action=check&value=" + encodeURI(vName) + "", //条件 
                {success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("该客户名可以使用!");
                            $('#State').focus();
                        } else {
                            $('#CustomerName').focus();
                            document.getElementById("CustomerName").value = "";
                            alert(str);
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                }, failure: function () { }
            });
        }
    } else {
        alert("客户名不能为空!");
        $('#CustomerName').focus();
    }
}
//检查决策人手机是否已存在
function CheckDecisionTel(type) {
    var id = "00000000-0000-0000-0000-000000000000";
    var vDecisionTel = "";
    if (type == 1) {
        vDecisionTel = $('#DecisionTel').val();
    } else {
        vDecisionTel = $('#DecisionTel2').val()
    }
    var vValue = vDecisionTel;
        if (vValue != "") {
            if (flag != "0") {
                id = $("#ID").val();
            }
            YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/CustomOperating.aspx?r=" + Math.random() + "&id=" + id + "&temp=" + flag + "&type=1&entity=CheckCustomerOld&action=check&value=" + encodeURI(vValue) + "", //条件 
            {success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") {
                        alert("该手机可以使用!");
                        $('#DecisionPosition').focus();
                    } else {
                        if (type == 1) {
                            $('#DecisionTel').focus();
                            document.getElementById("DecisionTel").value = "";
                        } else {
                            $('#DecisionTel2').focus();
                            document.getElementById("DecisionTel2").value = "";
                        }
                        alert(str);
                    }
                }
                catch (e) {
                }
                finally {
                }
            }, failure: function () { }
        });
    } else {
        if (type == 1) {
            alert("决策人手机1不能为空!");
            $('#DecisionTel').focus();
        } else {
            alert("决策人手机2不能为空!");
            $('#DecisionTel2').focus();
        }
    }
}

//保存方法
var Save = function (obj) {
    //debugger
    instance[0].ID = $('#ID').val().replace(/(^\s*)|(\s*$)/g, '');
    instance[0].CustomerName = $('#CustomerName').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].Address = $('#Address').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].State = $("#State").find("option:selected").text();
    instance[0].Province = $("#Province").find("option:selected").text();
    instance[0].City = $("#City").find("option:selected").text();
    instance[0].Area = $("#Area").find("option:selected").text();
    instance[0].Decisionmaker = $('#Decisionmaker').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].DecisionTel1 = $('#DecisionTel').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].DecisionTel2 = $('#DecisionTel2').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].DecisionPosition = $('#DecisionPosition').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].DecisionSex = $('#DecisionSex').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].CompanyProduct = $('#CompanyProduct').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].Tel1 = $('#Tel1').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].PersonId = $('#PersonId').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].PersonName = $("#PersonId").find("option:selected").text();
    instance[0].AreaId = $('#AreaId').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].AreaName = $("#txtAreaId").val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].TradeId = $('#TradeId').val().replace(/(^\s*)|(\s*$)/g, '');
    instance[0].TradeName = $("#txtTradeId").val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].ProductId = $('#ProductId').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].ProductName = $("#ProductId").find("option:selected").text();
    instance[0].PropertyId = $('#PropertyId').val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    instance[0].PropertyName = $("#PropertyId").find("option:selected").text();
    instance[0].Explains = $("#Explains").val().replace(/(^\s*)|(\s*$)/g, '').replace(/\s/g, "");
    if (instance[0].CustomerName == "") {
        alert("客户名称不能为空!");
        $('#CustomerName').focus();
        return;
    }
    if (instance[0].CustomerName.length < 8) {
        alert("客户名的长度不能少于8个字,请输入公司全称!\n如：深圳市中旭企业管理股份有限公司");
        $('#CustomerName').focus();
        return;
    }
    if (instance[0].Province == "" || instance[0].Province == "选择省份") {
        alert("省份不能为空!");
        $('#Province').focus();
        return;
    }
    if (instance[0].City == "" || instance[0].City == "选择城市") {
        alert("城市不能为空!");
        $('#City').focus();
        return;
    }
    if (instance[0].Province == "选择省份") {
        instance[0].Province = "";
    }
    if (instance[0].City == "选择城市") {
        instance[0].City = "";
    }
    if (instance[0].Area == "选择区县") {
        instance[0].Area = "";
    }
    if (instance[0].Address == "") {
        alert("地址不能为空!");
        $('#Address').focus();
        return;
    }
    if (instance[0].AreaId == "" || instance[0].AreaId == null || instance[0].AreaId == "00000000-0000-0000-0000-000000000000") {
        alert("客户区域不能为空!");
        $('#AreaId').focus();
        return;
    }
    if (obj == 0) {
        if (instance[0].Decisionmaker == "") {
            alert("决策人不能为空!");
            $('#Decisionmaker').focus();
            return;
        }
        if (instance[0].Decisionmaker.length < 2) {
            alert("决策人姓名不能为单个汉字!");
            $('#Decisionmaker').focus();
            return;
        }
        if (instance[0].DecisionTel1 == "") {
            alert("决策人手机不能为空!");
            $('#DecisionTel').focus();
            return;
        }
        if (instance[0].DecisionPosition == "") {
            alert("决策人职务不能为空!");
            $('#DecisionPosition').focus();
            return;
        }
        if (instance[0].DecisionSex == "" || instance[0].DecisionSex == null) {
            alert("决策人性别不能为空!");
            $('#DecisionSex').focus();
            return;
        }
        if (instance[0].CompanyProduct == "") {
            alert("主营业务不能为空!");
            $('#CompanyProduct').focus();
            return;
        }
        if (instance[0].Tel1 == "") {
            alert("公司电话1不能为空!");
            $('#Tel1').focus();
            return;
        }
        if (instance[0].PersonId == "" || instance[0].PersonId == null || instance[0].PersonId == "00000000-0000-0000-0000-000000000000") {
            alert("员工人数不能为空!");
            $('#PersonId').focus();
            return;
        }
        if (instance[0].TradeId == "" || instance[0].TradeId == null || instance[0].TradeId == "00000000-0000-0000-0000-000000000000") {
            alert("所属行业不能为空!");
            $('#txtTradeId').focus();
            return;
        }
        if (instance[0].ProductId == "" || instance[0].ProductId == null || instance[0].ProductId == "00000000-0000-0000-0000-000000000000") {
            alert("公司产值不能为空!");
            $('#ProductId').focus();
            return;
        }
        if (instance[0].PropertyId == "" || instance[0].PropertyId == null || instance[0].PropertyId == "00000000-0000-0000-0000-000000000000") {
            alert("公司性质不能为空!");
            $('#PropertyId').focus();
            return;
        } 
    }
    if (obj == 0) {   //保存并转到公海
        if (confirm("是否确认保存并转到公海操作？")) {
            SaveData(obj);
        }
    } else if (obj == 1) {  //保存
        if (confirm("是否确定保存?")) {
            SaveData(obj);
        }
    }
}
var SaveData = function (obj) {
    var json = $.toJSON(instance[0]).replace(/'/g, "[B]");
    $("#jsonDataContent").val(json);
    var formObject = document.getElementById('form2');
    YAHOO.util.Connect.setForm(formObject);
    var cObj = YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/CustomOperating.aspx?flag=1&type=" + flag + "&object=" + obj + "&action=update&entity=CrmCustomerOldUpdate", callback);
}
var responseSuccess = function (res) {
    try {
        var str = res.responseText;
        if (str == "1") {
            alert("数据保存成功");
            //RefreshDataParent();
            closeWin();
        }
        else {
            alert(str);
        }
    } catch (e) {
    }
    finally {
    }
};
var responseFailure = function (error) {
    alert("数据错误：" + error.statusText);
}
var callback = {
    success: responseSuccess,
    failure: responseFailure
};
//刷新父窗体数据
function RefreshDataParent() {
    if (parent.NeedRefreshPage != null) {
        if (typeof (parent.NeedRefreshPage.LoadData) == "function") {
            parent.NeedRefreshPage.RefreshData();
        }
    }
}
function ReLoadData() {
}

//保存并转到公海
var SaveToCustomer = function () {
    Save(0);
}
var SaveCustomer = function () {
    Save(1);
}
//废弃
var SaveRubbish = function () {
    document.getElementById("EditUIWindow1").src = "../../Crm/Customer/CustomerToRubbish.aspx?r=" + Math.random() + "&id=" + getPar1("id");
    YAHOO.example.container.dialog1.show();
}
//终止
var SaveStop = function () {
    if (confirm("是否确认终端该客户？")) {
        var Url = "../../Extend/AjaxData/CustomOperating.aspx?r=" + Math.random() + "&action=update&entity=CrmCustomerOldStop&json=&id=" + getPar1("id");
        YAHOO.util.Connect.asyncRequest('POST', Url, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") {
                        alert("操作成功!");
                        closeWin();
                        //RefreshDataParent();
                    }
                    else {
                        alert(str);
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
}
//返回
var SaveReturn = function () {
    //RefreshDataParent();
    closeWin();
}